// POST /api/contact - 处理联系表单提交
export default defineEventHandler(async (event) => {
  const body = await readBody(event)
  
  // 验证表单数据
  const { name, email, subject, message } = body
  
  if (!name || !email || !subject || !message) {
    throw createError({
      statusCode: 400,
      statusMessage: '请填写所有必填字段'
    })
  }
  
  // 验证邮箱格式
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  if (!emailRegex.test(email)) {
    throw createError({
      statusCode: 400,
      statusMessage: '邮箱格式不正确'
    })
  }
  
  try {
    // 这里可以添加实际的邮件发送逻辑
    // 例如：发送邮件、保存到数据库等
    
    console.log('收到联系表单:', {
      name,
      email,
      subject,
      message,
      timestamp: new Date().toISOString()
    })
    
    // 模拟处理时间
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    return {
      success: true,
      message: '消息发送成功！我们会尽快回复您。'
    }
  } catch (error) {
    console.error('处理联系表单时出错:', error)
    
    throw createError({
      statusCode: 500,
      statusMessage: '发送失败，请稍后重试'
    })
  }
}) 